﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tenebrous_Manor.Navigation
{
    class Triggers
    {
        public static int shadeForm = 0;
        public static float speed = 0.5f;

        public String CheckTrigger(ref Dialogue.Dialogue_keys[,] array, Room current_room, Facing facing, int x, int y)
        {
            Dialogue.Dialogue_keys current_key = Dialogue.Dialogue_keys.NOTHING;
            switch (facing)
            {
                case Facing.UP:
                    current_key = array[x, y - 1];
                    break;
                case Facing.DOWN:
                    current_key = array[x, y + 1];
                    break;
                case Facing.LEFT:
                    current_key = array[x - 1, y];
                    break;
                case Facing.RIGHT:
                    current_key = array[x + 1, y];
                    break;
            }

            if (array[x, y].Equals(Dialogue.Dialogue_keys.BROKEN_DOOR)
                || array[x, y].Equals(Dialogue.Dialogue_keys.SCREWDRIVER))
                current_key = array[x, y];

            CheckSpecialTriggers(ref current_key, current_room);

            ResearchNote(ref current_key, current_room, facing, x, y);

            return Dialogue.dialogue[current_key];
        }

        public void CheckSpecialTriggers(ref Dialogue.Dialogue_keys current_key, Room current_room)
        {
            switch (current_room)
            {
                #region 1st floor
                case Room.KITCHEN:
                    if (current_key.Equals(Dialogue.Dialogue_keys.KITCHEN_LEVER))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.STUDY_NOTE))
                            GameBools.SetBool(GameBools.GameBool.LEVER_PULLED, true);
                    }
                    else if (current_key.Equals(Dialogue.Dialogue_keys.FRIDGE))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.SEEN_NUM_LOCK))
                        {
                            GameBools.SetBool(GameBools.GameBool.HAVE_COMBINATION, true);
                            speed = 1.0f;
                            shadeForm = 2;
                        }
                    }
                    break;

                case Room.JUNK_ROOM:
                    if(current_key.Equals(Dialogue.Dialogue_keys.SCREWDRIVER))
                    {
                         if (GameBools.getGameBool(GameBools.GameBool.SEEN_MISSING_KNOB))
                            GameBools.SetBool(GameBools.GameBool.SCREWDRIVER, true);
                    } 
                    else if (current_key.Equals(Dialogue.Dialogue_keys.STUDY_DOOR))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.STUDY_KEY))
                        {
                            GameBools.SetBool(GameBools.GameBool.STUDY, true);
                            shadeForm = 1;
                            speed = 0.7f;
                        }
                    }
                    break;

                case Room.STUDY:
                    if (current_key.Equals(Dialogue.Dialogue_keys.NOTE))
                    {
                        GameBools.SetBool(GameBools.GameBool.STUDY_NOTE, true);
                    }
                    else if (current_key.Equals(Dialogue.Dialogue_keys.STUDY_DESK))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.LEVER_PULLED))
                        {
                            GameBools.SetBool(GameBools.GameBool.DESK_MOVED, true);
                            speed = 0.85f;
                        }
                    }
                    break;
                #endregion

                #region 2nd floor
                case Room.UPPER_LANDING:
                    if(current_key.Equals(Dialogue.Dialogue_keys.BROKEN_DOOR))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.SEEN_MISSING_KNOB))
                            GameBools.SetBool(GameBools.GameBool.DOORKNOB, true);
                    }
                    else if (current_key.Equals(Dialogue.Dialogue_keys.MISSING_DOORKNOB))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.SCREWDRIVER)
                            && GameBools.getGameBool(GameBools.GameBool.DOORKNOB))
                        {
                            GameBools.SetBool(GameBools.GameBool.BEDROOM, true);
                            speed = 0.65f;
                        }

                        GameBools.SetBool(GameBools.GameBool.SEEN_MISSING_KNOB, true);
                    }
                    break;

                case Room.BEDROOM:
                    if (current_key.Equals(Dialogue.Dialogue_keys.STUDY_KEY))
                    {
                        GameBools.SetBool(GameBools.GameBool.STUDY_KEY, true);
                    }
                    break;
                #endregion

                #region basement
                case Room.BASEMENT:
                    if (current_key.Equals(Dialogue.Dialogue_keys.LAB_DOOR))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.SEEN_NUM_LOCK)
                            && GameBools.getGameBool(GameBools.GameBool.HAVE_COMBINATION))
                            GameBools.SetBool(GameBools.GameBool.RESEARCH_LAB, true);
                        else
                            GameBools.SetBool(GameBools.GameBool.SEEN_NUM_LOCK, true);
                    }
                    break;

                case Room.RESEARCH_LAB:
                    if (current_key.Equals(Dialogue.Dialogue_keys.BREAKER))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.FUSE))
                            GameBools.SetBool(GameBools.GameBool.END, true);
                        else
                            GameBools.SetBool(GameBools.GameBool.BREAKER, true);
                    }
                    else if (current_key.Equals(Dialogue.Dialogue_keys.FUSE))
                    {
                        if (GameBools.getGameBool(GameBools.GameBool.BREAKER))
                            GameBools.SetBool(GameBools.GameBool.FUSE, true);
                    }
                    break;
                #endregion
            }
        }

        public void ResearchNote(ref Dialogue.Dialogue_keys current_key, Room current_room, Facing facing, int x, int y)
        {
            foreach (Randomization.ResearchNote note in Randomization.RandomResearchNotes.research_notes)
            {
                if (!note.isFound())
                {
                    if (note.getRoom().Equals(current_room))
                    {
                        int observed_x = x;
                        int observed_y = y;

                        #region facing switch
                        switch (facing)
                        {
                            case Facing.UP:
                                observed_x = x;
                                observed_y = y - 1;
                                break;
                            case Facing.DOWN:
                                observed_x = x;
                                observed_y = y + 1;
                                break;
                            case Facing.LEFT:
                                observed_x = x - 1;
                                observed_y = y;
                                break;
                            case Facing.RIGHT:
                                observed_x = x + 1;
                                observed_y = y;
                                break;
                        }
                        #endregion

                        if ((note.getX() == observed_x && note.getY() == observed_y)
                            || (note.getX() == x && note.getY() == y))
                        {
                            current_key = Dialogue.Dialogue_keys.FOUND_NOTE;
                            note.foundNote();
                        }

                    }
                }
            }
        }

        public int getShadeForm()
        {
            return shadeForm;
        }

        public float getSpeed()
        {
            return speed;
        }

    }
}
